//
//  moreActionView.m
//  Follow Me
//
//  Created by apple on 2017/4/22.
//  Copyright © 2017年 apple. All rights reserved.
//

#import "moreActionView.h"
#import "MainModel.h"
#import "Routes+CoreDataClass.h"
#import "Photos+CoreDataClass.h"


@implementation moreActionView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (void)awakeFromNib
{
    [super awakeFromNib];
    self.backgroundColor = [UIColor clearColor];
    UITapGestureRecognizer *gr = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapBackground)];
    [self addGestureRecognizer:gr];
}

- (void)tapBackground
{
    [self removeFromSuperview];
}

- (IBAction)shareClickButton:(id)sender {
    
    [self removeFromSuperview];
    Routes *routeInfo = [MainModel model].routeMsg;
    NSNumber *keyNum = [NSNumber numberWithInt:routeInfo.keyID];
    NSArray *photos = [Photos findByAttribute:@"rouID" withValue:keyNum];
//    Photos *photoInfo = [photos lastObject];
    NSMutableArray *kArray = [[NSMutableArray alloc]init];
    for (id obj in photos)
    {
        Photos *everyPhoto = obj;
        UIImage *temPho = [UIImage imageWithData:everyPhoto.photoData];
        [kArray addObject:temPho];
    }
    UIImage *cover = [UIImage imageWithData:routeInfo.introducePicture];
    [kArray addObject:cover];
//    NSArray *teArray = [kArray copy];
    //图片数组
    NSArray *imageArray = [kArray copy];
    
    NSMutableArray *mArray = [[NSMutableArray alloc]init];
    
    int i = 0;
    for (UIImage *image  in imageArray) {
        
        //本地路径
        NSString *str = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).firstObject;
        NSString *filePath = [NSString stringWithFormat:@"%@%d.jpg",str,i];
        //保存本地
        [UIImagePNGRepresentation(image) writeToFile:filePath atomically:YES];
        
        NSURL *fileURL = [NSURL fileURLWithPath:filePath];
        
        //把图片和路径传进来
        MJShareItem *item = [[MJShareItem alloc] initWithImage:image andfile:fileURL];
        [mArray addObject:item];
        i++;
    }
    
    //里面initWithActivityItems  传的是item的数组  如果直接用图片数组的话 会经常出现 微信断开的错误
    UIActivityViewController *activityView =[[UIActivityViewController alloc] initWithActivityItems:mArray
                                                                              applicationActivities:nil];
    
    //    //需要忽略的分享
    //    activityView.excludedActivityTypes = @[UIActivityTypePrint, UIActivityTypeCopyToPasteboard,UIActivityTypeAssignToContact,UIActivityTypeSaveToCameraRoll,UIActivityTypePostToFacebook,UIActivityTypePostToTwitter,
    //                                           UIActivityTypePostToWeibo
    //                                           ,UIActivityTypeMessage
    //                                           ,UIActivityTypeMail,UIActivityTypePrint,UIActivityTypeCopyToPasteboard,UIActivityTypeAssignToContact,
    //                                           UIActivityTypeSaveToCameraRoll,UIActivityTypeAddToReadingList,UIActivityTypePostToFlickr,
    //                                           //                                            UIActivityTypePostToVimeo,UIActivityTypePostToTencentWeibo,UIActivityTypeAirDrop,UIActivityTypeOpenInIBooks
    //                                           ];
    
    activityView.restorationIdentifier = @"activity";
    [activityView setTitle:@"分享"];
    UIViewController *currentTopVC = [self currentTopViewController];
    [currentTopVC presentViewController:activityView animated:TRUE completion:nil];
    

    
}
//present A viewcontroller in A uiview
- (UIViewController *)currentTopViewController {
    UIViewController *topVC = [[[[UIApplication sharedApplication] delegate] window] rootViewController];
    while (topVC.presentedViewController) {
        topVC = topVC.presentedViewController;
    }
    return topVC;
}








@end
